package com.example.i.spy;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HintsScreen extends Activity {

	private boolean hint2Clicked = false;
	private boolean hint3Clicked = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_hints_screen);
        
        // objectName is the object name the initiator inputed.
        // displayedObjectName is how the object name will be displayed
        // after using hint 2 and/or 3.
        final String objectName = getIntent().getExtras().getString("objectName");
        
        // Hint 1 is just the hint that the initiator inputed.
        TextView hint1 = (TextView) findViewById(R.id.hint1_text);
        hint1.setText(getIntent().getExtras().getString("hint"));

		// Hint 2 will display the number of letters in the object name.
		final Button hint2Button = (Button)findViewById(R.id.hint2_button);
		
		// Hint 3 will display the first letter of the object name.
		final Button hint3Button = (Button)findViewById(R.id.hint3_button);
        
		// Get the extras that will determine if the guesser already "bought"
		// the hints.
        hint2Clicked = getIntent().getExtras().getBoolean("hint2Clicked");
        hint3Clicked = getIntent().getExtras().getBoolean("hint3Clicked");
        
        if (hint2Clicked && hint3Clicked)
        	hints2and3(objectName);
        
        else if (hint2Clicked && !hint3Clicked)
        	hint2(objectName);
        
        else if (!hint2Clicked && hint3Clicked)
        	hint3(objectName);
        
		// functionality for the how to back button
		Button helpButton = (Button) findViewById(R.id.back_button);
		helpButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) 
			{
				Intent i = new Intent();

				i.putExtra("hint2Clicked", hint2Clicked);
				i.putExtra("hint3Clicked", hint3Clicked);
				
		    	setResult(RESULT_OK, i);
				finish();
			}
		});
		
		// Hint 2 will display the number of letters in the object name.
		hint2Button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) 
			{
				if(hint3Clicked == false)
					hint2(objectName);
				
				else
					hints2and3(objectName);
			}
		});

		// Hint 3 will display the first letter of the object name.
		hint3Button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) 
			{
				if(hint2Clicked == false)
					hint3(objectName);
				
				else
					hints2and3(objectName);
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_hints_screen, menu);
        return true;
    }
    
    private void hint2(String objectName)
    {
		//final Button hint2Button = (Button)findViewById(R.id.hint2_button);
		//hint2Button.setVisibility(View.INVISIBLE);
		hint2Clicked = true;
		
        final TextView displayedObjectName = (TextView) findViewById(R.id.object_name_text);
        
		int numberOfLetters = objectName.length();
		String temp = "";
		
		for(int i = 0; i < numberOfLetters; i++)
		{
			temp += "_ ";
		}
			
		displayedObjectName.setText(temp);
    }
    
    private void hint3(String objectName)
    {
		//final Button hint3Button = (Button)findViewById(R.id.hint3_button);
		//hint3Button.setVisibility(View.INVISIBLE);
		hint3Clicked = true;
		
        final TextView displayedObjectName = (TextView) findViewById(R.id.object_name_text);
        
		String temp = "";
		temp = objectName.substring(0, 1) + "...";
		
		displayedObjectName.setText(temp);
    }
    
    private void hints2and3(String objectName)
    {
		//final Button hint2Button = (Button)findViewById(R.id.hint2_button);
		//hint2Button.setVisibility(View.INVISIBLE);
		hint2Clicked = true;

		//final Button hint3Button = (Button)findViewById(R.id.hint3_button);
		//hint3Button.setVisibility(View.INVISIBLE);
		hint3Clicked = true;
		
        final TextView displayedObjectName = (TextView) findViewById(R.id.object_name_text);
        
		int numberOfLetters = objectName.length();
		String temp = "";
		
		temp = objectName.substring(0, 1) + " ";
		
		for(int i = 0; i < numberOfLetters - 1; i++)
		{
			temp += "_ ";
		}
		
		displayedObjectName.setText(temp);
    }
}
